package mbp.gui.panels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JPanel;

import mbp.base.GuiFunction;
import mbp.core.ResourceUtility;
import mbp.exceptions.InvalidEnumTypeException;
import mbp.exceptions.InvalidFeatureNameException;
import mbp.gui.base.IListPanel;

import javax.swing.JScrollPane;
import java.awt.Component;
import javax.swing.JButton;
import java.awt.Font;
import java.util.Iterator;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import org.apache.log4j.Logger;

public class ResourceUtilityEditPanel extends JPanel implements ActionListener
{
	private ResourceUtility resourceUtility;
	private JTextField txtUtilityFunction;
	private JButton btnSave;
	private JTable dtUtilities;
	private DefaultTableModel modelUtilities;
	private Logger logger = Logger.getLogger(this.getClass());
	private JTextField txtType;
	
	private IListPanel listPanel;

	/**
	 * Create the panel.
	 */
	private ResourceUtilityEditPanel() 
	{
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane((Component) null);
		scrollPane.setBounds(10, 121, 270, 101);
		add(scrollPane);
		
		modelUtilities = new DefaultTableModel();
		modelUtilities.addColumn("Feature Name");
		modelUtilities.addColumn("Enum Value");
		modelUtilities.addColumn("Int Value");
		
		dtUtilities = new JTable(modelUtilities);
		dtUtilities.setFillsViewportHeight(true);
		scrollPane.setViewportView(dtUtilities);
		
		btnSave = new JButton("Save");
		btnSave.setName("UT_SET");
		btnSave.setFont(new Font("Dialog", Font.PLAIN, 11));
		btnSave.setBounds(200, 34, 80, 23);
		add(btnSave);
		btnSave.addActionListener(this);
		
		JLabel lblEnumItems = new JLabel("ENUM Items - values");
		lblEnumItems.setFont(new Font("Dialog", Font.PLAIN, 12));
		lblEnumItems.setBounds(10, 106, 150, 14);
		add(lblEnumItems);
		
		txtUtilityFunction = new JTextField();
		txtUtilityFunction.setBounds(10, 75, 270, 20);
		add(txtUtilityFunction);
		
		JLabel label_1 = new JLabel("Utility expression");
		label_1.setFont(new Font("Dialog", Font.PLAIN, 12));
		label_1.setBounds(10, 61, 150, 14);
		add(label_1);
		
		txtType = new JTextField();
		txtType.setEditable(false);
		txtType.setBounds(10, 36, 86, 20);
		add(txtType);
		txtType.setColumns(10);
		
		JLabel lblType = new JLabel("Type");
		lblType.setBounds(10, 21, 46, 14);
		add(lblType);

	}
	
	public ResourceUtilityEditPanel(GuiFunction guiFunction, IListPanel listPanel)
	{
		this();
		this.listPanel = listPanel;
		if(guiFunction == GuiFunction.VIEWER)
		{
			btnSave.setVisible(false);
			txtUtilityFunction.setEditable(false);
			dtUtilities.setEnabled(false);
		}
	}
	
	public void clear()
	{
		this.resourceUtility = null;
		txtType.setText("");
		txtUtilityFunction.setText("");
		/**
		 * Wyczyszczenie tabeli
		 */
		for( int i = modelUtilities.getRowCount() - 1; i >= 0; i-- )
		{
			modelUtilities.removeRow(i);
		}
	}
	
	public void LoadUtilityFunction(ResourceUtility resourceUtility)
	{
		this.resourceUtility = resourceUtility;
		int rowsCount = 0;
		
		txtUtilityFunction.setText(resourceUtility.getExrpession());
		txtType.setText(resourceUtility.getType().getResourceTypeName());

		/**
		 * Wyczyszczenie tabeli
		 */
		for( int i = modelUtilities.getRowCount() - 1; i >= 0; i-- )
		{
			modelUtilities.removeRow(i);
		}
		
		Iterator<String> it1 = resourceUtility.getTableNames().iterator();
		while(it1.hasNext())
		{
			String key = it1.next();
			
			Iterator<String> it2 = resourceUtility.getTableValues(key).keySet().iterator();
			
			while(it2.hasNext())
			{
				String en = it2.next();
				Integer v = resourceUtility.getTableValues(key).get(en);
				
				modelUtilities.insertRow(rowsCount, new Object[]{key, en, v.toString()});
				rowsCount++;
			}
			
		}
	}
	
	public void setUtilityFunction()
	{
		resourceUtility.setExpression(txtUtilityFunction.getText());
		//modelUtilities.
		
		for(int i = 0; i<modelUtilities.getRowCount(); i++)
		{
			String fType = (String)modelUtilities.getValueAt(i, 0);
			String en = (String)modelUtilities.getValueAt(i, 1);
			Integer iValue = Integer.parseInt((String) modelUtilities.getValueAt(i, 2));
			
			try {
				resourceUtility.setEnumValue(fType, en, iValue);
			} catch (InvalidFeatureNameException e1) {
				// TODO Auto-generated catch block
				logger.error("InvalidFeatureNameException", e1);
			} catch (InvalidEnumTypeException e1) {
				// TODO Auto-generated catch block
				logger.error("InvalidEnumTypeException", e1);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getSource() == btnSave)
		{
			setUtilityFunction();
			if(listPanel != null)
			{
				listPanel.reloadList();
			}
		}
		
	}
}
